package com.fauks.ee.full_text_retrieva.solr;

public class SolrJDemo {

    /*//添加索引
    @Test
    public void testAddIndex() throws IOException, SolrServerException {
        //单机版 域名/访问哪个核
        HttpSolrClient solrClient = new HttpSolrClient.Builder("http://192.168.23.133:8080/solr7/taotao_core")
                .withConnectionTimeout(10000)
                .withSocketTimeout(60000)
                .build();
        SolrInputDocument doc = new SolrInputDocument();
        //id相同就是更新，id不存在则为添加
        doc.addField("id", "test");
        doc.addField("name", "fauks");
        UpdateResponse add = solrClient.add(doc);
        System.out.println(add.getElapsedTime());
        //手动提交
        solrClient.commit();
    }

    //删除
    @Test
    public void testDeleteIndex() throws IOException, SolrServerException {
        HttpSolrClient solrClient = new HttpSolrClient.Builder("http://192.168.23.133:8080/solr7/core1")
                .withConnectionTimeout(10000)
                .withSocketTimeout(60000)
                .build();
        //按照条件删除 *:*删除所有
        solrClient.deleteByQuery("id:test", 1000);
        //手动提交
        solrClient.commit();
    }

    //查询 （如果不分页 java代码默认查询10条）
    @Test
    public void testQueryIndex() throws IOException, SolrServerException {
        //关键词：台灯 过滤条件：价格排序、分页

        HttpSolrClient solrClient = new HttpSolrClient.Builder("http://192.168.23.133:8080/solr7/core1")
                .withConnectionTimeout(10000)
                .withSocketTimeout(60000)
                .build();

        SolrQuery params = new SolrQuery();
        //设置关键词
        //params.set("q", "product_name:台灯");
        params.setQuery("product_name:台灯");
        //要求属于幽默杂货过滤条件
        //params.set("fq", "product_catagory_name:雅致灯饰");
        //100以上价格
        params.set("fq", "product_catagory_name:[100 TO *]");

        //排序
        params.addSort("product_price", SolrQuery.ORDER.desc);
        //分页
        params.setStart(0);
        params.setRows(5);
        //默认域
        params.set("df", "product_name");
        //指定返回的域(只返回id，name)
        params.set("fl", "id,product_name");
        //高亮
        params.setHighlight(true);//开启高亮
        //指定高亮字段
        params.addHighlightField("product_name");
        //前缀、后缀 css样式
        params.setHighlightSimplePre("<span stype='color:red'>");
        params.setHighlightSimplePost("</span>");
        //执行查询
        QueryResponse response = solrClient.query(params);
        SolrDocumentList results = response.getResults();
        //高亮与普通是两个容器
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //map k id V map(k域名 vlist->get(0))
        long numFound = results.getNumFound();
        System.out.println("总条数:" + numFound);
        for (SolrDocument result : results) {
            String id = (String) result.get("id");
            System.out.println(id);
            System.out.println(result.get("product_name"));
            Map<String, List<String>> stringListMap = highlighting.get(id);
            List<String> product_name = stringListMap.get("product_name");
            System.out.println(product_name.get(0));
        }

        solrClient.commit();
    }*/
}
